﻿//Shashikant-012
using System;
using System.Web;
using System.Web.Security; 
using System.Collections.Generic;
using System.Linq;

public partial class Login : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            txtUsername.Focus();
            FormsAuthentication.SignOut();
            Session.Clear();
            Session.Abandon();
        }
    }

    #region UI Events
    protected void cmdLogin_Click(object sender, EventArgs e)
    {
        if (AuthenticateUser())
        {
            #region .....Create Authentication Tickets
            FormsAuthenticationTicket objTicket = null;
            HttpCookie objCookie = null;
            objTicket = new FormsAuthenticationTicket(1, SessionInfo.prop_getObj.EmployeeID.ToString(), System.DateTime.Now, DateTime.Now.AddMinutes(480), false, "users");
            objCookie = new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(objTicket));
            HttpContext.Current.Response.Cookies.Add(objCookie);
            UI_fnLoadUserMenu();
            Response.Redirect("Default.aspx");
            #endregion
        }
        else
        {
            lblErrorMsg.Text = "Email id and or password incorrect.";
            ClientScript.RegisterStartupScript(typeof(string), "KeyLoginError", "$('#divError').show();", true);
        }
    }
    #endregion

    #region Program Functions
    /// <summary>
    /// Authenticate user by user email address, password and branch
    /// </summary>
    /// <returns>return true if user credential is correct else false</returns>
    private bool AuthenticateUser()
    {
        PLMstUser UI_objUser = new PLMstUser();

        UI_objUser.UserEmail = txtUsername.Text;
        UI_objUser.UserLoginPassword = txtPassword.Text;
        UI_objUser = new BL_User().BLValidateUserLogin(UI_objUser);

        if (UI_objUser.IsLoginSuccess == true)
        {
            SessionInfo.prop_getObj.EmployeeID = UI_objUser.MstUserID;
            SessionInfo.prop_getObj.EmployeeFullName = UI_objUser.UserFullName;

            SessionInfo.prop_getObj.EmpEmail = UI_objUser.UserEmail;
            SessionInfo.prop_getObj.IsEmployeeAdmin = UI_objUser.IsAdmin.GetValueOrDefault();
            return true;
        }
        else
        {
            return false;
        }

    }

    /// <summary>
    /// Load the user menu session object
    /// </summary>
    private void UI_fnLoadUserMenu()
    {
        PLMstUser objEmp = new PLMstUser();
        objEmp.MstUserID = SessionInfo.prop_getObj.EmployeeID;
        objEmp.IsAdmin = SessionInfo.prop_getObj.IsEmployeeAdmin;

        List<PLMstFormMasters> objList = new BL_User().BL_fnGetEmployeeMenu(objEmp);
        SessionInfo.prop_getObj.Menu = objList;
    }

    protected void btnForgotPassword_Click(object sender, EventArgs e)
    {  
        PLMstUser objEmployee = new BL_User().BL_fnGetUserByEmailID(new PLMstUser() { UserEmail = txtForgotEmail.Text.Trim() });
        if (objEmployee != null)
        {
            if (objEmployee.IsActive == true)
            {
                Email_Stack objEmail = new Email_Stack();
                objEmail.FromAddress = System.Configuration.ConfigurationManager.AppSettings["FromEmailID"];
                objEmail.ToName = "";
                objEmail.ToAddress = objEmployee.UserEmail;
                objEmail.ToCCName = "";
                objEmail.ToCC = "";
                objEmail.Subject = "Auditor Billing - Forgot Password";
                objEmail.Body = "<div " + Email.GetTableStyle()[0] + ">" + "Your Auditor Billing Password is: " + objEmployee.UserLoginPassword + "</div>";
          
                try
                {
                    
                    lblForgotPassSuccessMsg.Text = "Email send successfully.";
                    ClientScript.RegisterStartupScript(typeof(string), "KeyLoginError", "$('#divForgotPassError').hide();$('#divForgotPassSuccess').show();", true);
                }
                catch (Exception exChangePassword)
                {
                    CommonUtil.Elmah_SignalException(exChangePassword, this.Context, "Master Page - Method: UI_fnForgotPassword()");
                    lblForgotPassErrorMsg.Text = "Could not send email. Try again.";
                    ClientScript.RegisterStartupScript(typeof(string), "KeyLoginError", "$('#divForgotPassSuccess').hide();$('#divForgotPassError').show();", true);
                }
            }
            else
            {
                lblForgotPassErrorMsg.Text = "Account is inactive.";
                ClientScript.RegisterStartupScript(typeof(string), "KeyLoginError", "$('#divForgotPassSuccess').hide();$('#divForgotPassError').show();", true);
            }
        }
        else
        {
            lblForgotPassErrorMsg.Text = "Invalid email id.";
            ClientScript.RegisterStartupScript(typeof(string), "KeyLoginError", "$('#divForgotPassSuccess').hide();$('#divForgotPassError').show();", true);
        }
        divForgotPassword.Style.Add("display", "");
    }
    #endregion
}